package com.woniu.guoguo.component;


import com.woniu.guoguo.dao.UserMapper;
import com.woniu.guoguo.model.User;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

public class ShiroRealm extends AuthorizingRealm {
    @Autowired
    private UserMapper userMapper;
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//        Object primaryPrincipal=principals.getPrimaryPrincipal();
//        if(null==primaryPrincipal){
//            throw  new RuntimeException("请登录后操作");
//        }
//        User user=(User) primaryPrincipal;
//        Set<String> roles=userMapper.findRoleByUserId(user.getUserId());
////        Set<String> permissions=userMapper.findPermissionsByUserId(user.getUserid());
//        SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo();
//        simpleAuthorizationInfo.setRoles(roles);
////        simpleAuthorizationInfo.setStringPermissions(permissions);
//        return simpleAuthorizationInfo;
//    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken=(UsernamePasswordToken) token;
        System.out.println(usernamePasswordToken.getUsername());
        User user=userMapper.findUserByUserName(usernamePasswordToken.getUsername());
        System.out.println(user);
        return new SimpleAuthenticationInfo(user,user.getPassword().toCharArray(),getName());
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        Object primaryPrincipal=principals.getPrimaryPrincipal();
        if(null==primaryPrincipal){
            throw  new RuntimeException("请登录后操作");
        }
        return null;
    }
}
